/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.IO;

/**
 *
 * @author kylin
 */
import com.kylingroup.JavaFX.Task.*;
import java.awt.BorderLayout;

import javax.swing.JFrame;

import java.awt.*;

import javax.swing.JButton;

import javax.swing.JTextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.*;

import javax.swing.JProgressBar;

public class SwingFileTransferProgress1 extends JFrame{
    
    JButton target = new JButton();

    JTextField sT = new JTextField();

    JTextField tT = new JTextField();

    JButton btnRead = new JButton();

    JButton btnExit = new JButton();

    JButton source = new JButton();

    JProgressBar progress = new JProgressBar();

    public static void main(String s[]){
       SwingFileTransferProgress1 frame=new SwingFileTransferProgress1();
       frame.setSize(400,300);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.show();

    }
    public SwingFileTransferProgress1(){
        try{
            initUI();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    
    
    private void initUI() throws Exception {
        this.getContentPane().setLayout(null);
        
        //关闭按钮
        btnExit.setBounds(new Rectangle(225, 187, 122, 25));
        btnExit.setText("退出");
        btnExit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        
        });
        
        //复制按钮
        btnRead.setBounds(new Rectangle(58, 190, 122, 25));
        btnRead.setText("复制");
        btnRead.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread(){//开启一个新的线程
                    int c;
                    long m=0;
                    int n=0;
                    public void run(){
                        try{
                            File ff=new File(sT.getText());//open a file
                            FileInputStream fO = new FileInputStream(sT.getText());//设置文件输入输出流
                            FileOutputStream fW = new FileOutputStream(tT.getText());
                            progress.setIndeterminate(true);
                            m=ff.length();//get the length of the file
                            progress.setIndeterminate(false);
                            progress.setMaximum((int)m);
                            while(n<=m){//copy the file
                                fW.write(fO.read());
                                progress.setValue(n);
                                n++;
                            }
                        }catch(Exception ex) {}
                    }
                }.start();//线程开始
            }
        });

        tT.setEditable(false);

        tT.setText("");

        tT.setBounds(new Rectangle(195, 113, 159, 27));

        sT.setEditable(false);

        sT.setText("");

        sT.setBounds(new Rectangle(199, 61, 159, 27));

        source.setBounds(new Rectangle(60, 60, 112, 31));

        source.setText("源文件");

        source.addActionListener(new Pbt_source_actionAdapter(this));

        target.addActionListener(new Pbt_target_actionAdapter(this));

        progress.setBounds(new Rectangle(50, 228, 305, 18));

        this.getContentPane().add(btnExit);//添加组件

        this.getContentPane().add(btnRead);

        this.getContentPane().add(tT);

        this.getContentPane().add(sT);

        this.getContentPane().add(source);

        this.getContentPane().add(target);

        this.getContentPane().add(progress);

        target.setBounds(new Rectangle(55, 112, 122, 25));

        target.setText("目标路径");

    }

    

    public void source_actionPerformed(ActionEvent e) {//源文件按钮的动作

        FileDialog f1=new FileDialog(this,"load a file",FileDialog.LOAD);

        f1.show();

        sT.setText(f1.getDirectory()+f1.getFile());

        progress.setValue(0);

    }

    public void exit_actionPerformed(ActionEvent e) {//退出的按钮的动作

        System.exit(0);

    }

    public void target_actionPerformed(ActionEvent e) {//目标文件按钮的动作

        FileDialog f2=new FileDialog(this,"choose a fie",FileDialog.SAVE);

        f2.show();

        tT.setText(f2.getDirectory()+f2.getFile());

    }

//以下是各种组件的监听

class Pbt_target_actionAdapter implements ActionListener {

    private SwingFileTransferProgress1 adaptee;

    Pbt_target_actionAdapter(SwingFileTransferProgress1 adaptee) {

        this.adaptee = adaptee;

    }

    public void actionPerformed(ActionEvent e) {

        adaptee.target_actionPerformed(e);

    }

}



class Pbt_source_actionAdapter implements ActionListener {

    private SwingFileTransferProgress1 adaptee;

    Pbt_source_actionAdapter(SwingFileTransferProgress1 adaptee) {

        this.adaptee = adaptee;

    }

    public void actionPerformed(ActionEvent e) {

        adaptee.source_actionPerformed(e);

    }
    }
}